package net.tv90.bfmccore;

import com.tuershen.nbtlibrary.CompoundLibraryManager;
import com.tuershen.nbtlibrary.api.CompoundLibraryApi;
import net.tv90.bfmccore.battlepack.BattlepackCommand;
import net.tv90.bfmccore.battlepass.Battlepass;
import net.tv90.bfmccore.battlepass.BattlepassCommand;
import net.tv90.bfmccore.battlepass.BattlepassListener;
import net.tv90.bfmccore.command.*;
import net.tv90.bfmccore.game.*;
import net.tv90.bfmccore.game.ai.AISystem;
import net.tv90.bfmccore.game.view.GameModelViewHandler;
import net.tv90.bfmccore.game.view.GameViewListener;
import net.tv90.bfmccore.game.view.GameViewManager;
import net.tv90.bfmccore.helper.DebugHelper;
import net.tv90.bfmccore.listener.*;
import net.tv90.bfmccore.slot.SlotHelper;
import net.tv90.bfmccore.squad.SquadListener;
import net.tv90.bfmccore.squad.SquadPapiShow;
import net.tv90.bfmccore.squad.SquadShowTask;
import net.tv90.bfmccore.task.AutoBackupTask;
import net.tv90.bfmccore.task.BFMCMainTask;
import net.tv90.bfmccore.task.CombatAreaCheckTask;
import net.tv90.bfmccore.unlock.UnlockCommand;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;

import java.io.File;
import java.util.logging.Logger;

import static net.tv90.bfmccore.SlainListener.killerInfo_pool;
import static net.tv90.bfmccore.helper.FlanHelper.loadWeaponTypeData;
import static net.tv90.bfmccore.listener.AntiCheatListener.antiCheat_pool;
import static net.tv90.bfmccore.slot.SlotHelper.getDefaultSlotMap;
import static net.tv90.bfmccore.squad.Squad.initSquadMap;
import static net.tv90.bfmccore.util.HUD.HUDDelayedQueue.hud_pool;


public class BFMCCore extends JavaPlugin {

    public static Server Server;
    public static Logger Logger;
    public static BukkitScheduler Scheduler;
    public static BFMCCore plugin;
    public static final File PLUGIN_ROOT = new File("./plugins/BFMCCore");
    public static CompoundLibraryApi libraryApi;
    public static boolean fixMode = false;

    public static void loadConfigurations() {
//        try {
//            loadServiceStarData();
//            Bukkit.getConsoleSender().sendMessage("§e勤务之星数据已经加载");
//        } catch (Exception e) {
//            Bukkit.getConsoleSender().sendMessage("§c勤务之星数据未正常加载！");
//            e.printStackTrace();
//        }
        try {
            loadWeaponTypeData();
            Bukkit.getConsoleSender().sendMessage("§e武器类型数据已经加载");
        } catch (Exception e) {
            Bukkit.getConsoleSender().sendMessage("§c武器类型数据未正常加载！");
            e.printStackTrace();
        }
        try {
            SlotHelper.DefaultSlotData = getDefaultSlotMap();
            Bukkit.getConsoleSender().sendMessage("§e默认装备数据已经加载");
        } catch (Exception e) {
            Bukkit.getConsoleSender().sendMessage("§c默认装备数据未正常加载!");
            e.printStackTrace();
        }
    }

    /** 加载任务处理器，即并发功能的生命周期 */
    public void setScheduler() {
        try {
            GameModelViewHandler gameModelViewHandler = GameModelViewHandler.getInstance();
            Bukkit.getScheduler().runTaskTimer(this, new BFMCMainTask(), 20L, 20L);
            Bukkit.getScheduler().runTaskTimer(this, new AutoBackupTask(), 20L, 20L);
            Bukkit.getScheduler().runTaskTimer(this, new CombatAreaCheckTask(), 5L, 5L);
            Bukkit.getScheduler().runTaskTimer(this, new SquadShowTask(), 20L, 20L);
            Bukkit.getConsoleSender().sendMessage("§e任务处理器已经加载");
        } catch (Exception e) {
            Bukkit.getConsoleSender().sendMessage("§c任务处理器未正常加载！");
            e.printStackTrace();
        }
    }

    /** 加载小队功能 */
    private void loadSquad() {
        try {
            initSquadMap();
            new SquadPapiShow().register();
            Bukkit.getConsoleSender().sendMessage("§e小队功能已经加载");
        } catch (Exception e) {
            Bukkit.getConsoleSender().sendMessage("§c小队功能未正常加载!");
            e.printStackTrace();
        }
    }

    /** 加载命令处理器 */
    private void loadCommand() {
        try {
            if (Bukkit.getPluginCommand("radio") != null) {
                Bukkit.getPluginCommand("radio").setExecutor(new RadioCommand());
            }
            if (Bukkit.getPluginCommand("setrank") != null) {
                Bukkit.getPluginCommand("setrank").setExecutor(new RankCommand());
            }
            if (Bukkit.getPluginCommand("inst") != null) {
                Bukkit.getPluginCommand("inst").setExecutor(new ItemCommand());
            }
            if (Bukkit.getPluginCommand("ribbon") != null) {
                Bukkit.getPluginCommand("ribbon").setExecutor(new RibbonCommand());
            }
            if (Bukkit.getPluginCommand("slot") != null) {
                Bukkit.getPluginCommand("slot").setExecutor(new SlotCommand());
            }
            if (Bukkit.getPluginCommand("att") != null) {
                Bukkit.getPluginCommand("att").setExecutor(new AttachmentCommand());
            }
            if (Bukkit.getPluginCommand("mch") != null) {
                Bukkit.getPluginCommand("mch").setExecutor(new MCHCommand());
            }
            if (Bukkit.getPluginCommand("squad") != null) {
                Bukkit.getPluginCommand("squad").setExecutor(new SquadCommand());
            }
            if (Bukkit.getPluginCommand("sq") != null) {
                Bukkit.getPluginCommand("sq").setExecutor(new SquadCommand());
            }
            if (Bukkit.getPluginCommand("menu") != null) {
                Bukkit.getPluginCommand("menu").setExecutor(new SquadCommand());
            }
            if(Bukkit.getPluginCommand("dbg") != null) {
                Bukkit.getPluginCommand("dbg").setExecutor(new DebugHelper());
            }
            if(Bukkit.getPluginCommand("game") != null) {
                Bukkit.getPluginCommand("game").setExecutor(new GameCommand());
            }
            if(Bukkit.getPluginCommand("bp") != null) {
                Bukkit.getPluginCommand("bp").setExecutor(new BattlepackCommand());
            }
            if(Bukkit.getPluginCommand("fact") != null) {
                Bukkit.getPluginCommand("fact").setExecutor(new FactionCommand());
            }
            if(Bukkit.getPluginCommand("unlock") != null) {
                Bukkit.getPluginCommand("unlock").setExecutor(new UnlockCommand());
            }
            if(Bukkit.getPluginCommand("manage") != null) {
                Bukkit.getPluginCommand("manage").setExecutor(new ManageCommand());
            }
            if (Bukkit.getPluginCommand("ai") != null) {
                Bukkit.getPluginCommand("ai").setExecutor(new AiCommand());
            }
            if (Bukkit.getPluginCommand("report") != null) {
                Bukkit.getPluginCommand("report").setExecutor(new ReportCommand());
            }
            if(Bukkit.getPluginCommand("bps") != null) {
                Bukkit.getPluginCommand("bps").setExecutor(new BattlepassCommand());
            }
            Bukkit.getConsoleSender().sendMessage("§e命令处理器正常加载!");
        } catch (Exception e) {
            Bukkit.getConsoleSender().sendMessage("§c命令处理器未加载!");
        }
    }

    /** 加载事件监听器 */
    private void loadListener() {
        try {
            Bukkit.getPluginManager().registerEvents(new GameViewListener(), this);
            Bukkit.getPluginManager().registerEvents(new RadioListener(), this);
            Bukkit.getPluginManager().registerEvents(new RankUpListener(), this);
            Bukkit.getPluginManager().registerEvents(new ServiceStarListener(), this);
            Bukkit.getPluginManager().registerEvents(new RibbonListener(), this);
            Bukkit.getPluginManager().registerEvents(new SlainListener(), this);
            Bukkit.getPluginManager().registerEvents(new AntiCheatListener(), this);
            Bukkit.getPluginManager().registerEvents(new UnlockListener(), this);
            Bukkit.getPluginManager().registerEvents(new CooldownListener(), this);
            Bukkit.getPluginManager().registerEvents(new SquadListener(), this);
            Bukkit.getPluginManager().registerEvents(new AntiVehicleListener(), this);
            Bukkit.getPluginManager().registerEvents(new BattlepassListener(), this);
            Bukkit.getPluginManager().registerEvents(new AISystem(), this);
            Bukkit.getPluginManager().registerEvents(new NotBuildListener(), this);
            Bukkit.getPluginManager().registerEvents(new SupplyListener(), this);
            Bukkit.getPluginManager().registerEvents(new ReviveListener(), this);
            Bukkit.getConsoleSender().sendMessage("§e事件监听器正常加载!");
        } catch (Exception e) {
            Bukkit.getConsoleSender().sendMessage("§c事件监听器未加载!");
            throw new RuntimeException(e);
        }
    }

    /** 停止执行所有线程池中的线程 */
    private void unLoadThreadPool(){
        killerInfo_pool.shutdownNow();
        antiCheat_pool.shutdownNow();
        hud_pool.shutdownNow();
    }

    /** 加载游戏模块 */
    private void createGame(){
        try{
            GameResourceInitializer.init();
            GameManager gm = GameManager.getInstance();
            Bukkit.getPluginManager().registerEvents(gm, this);
            Bukkit.getScheduler().runTaskTimer(this, gm, 0L, 2L);
            Bukkit.getScheduler().runTaskTimer(this, GameViewManager.getInstance(), 0L, 20 / GameViewManager.TICK_RATE);
            gm.initGame();
            Bukkit.getConsoleSender().sendMessage("§e核心游戏模块已经加载");
        } catch (Exception e){
            Bukkit.getConsoleSender().sendMessage("§e核心游戏模块未正常加载");
            e.printStackTrace();
        }
    }

    @Override
    public void onEnable() {
        Logger = getLogger();
        Server = getServer();
        Scheduler = getServer().getScheduler();
        plugin = this;
        libraryApi = CompoundLibraryManager.getPluginManager(this);

        Bukkit.getConsoleSender().sendMessage("§e=========================================================");
        Bukkit.getConsoleSender().sendMessage("§e$$$$$$$$$$$$  $$$$$$$$$$$ $$$$$      $$$$$  $$$$$$$$$$$$");
        Bukkit.getConsoleSender().sendMessage("§e$$$$$$$$$$$$$ $$$$$$$$$$$ $$$$$$    $$$$$$ $$$$$$$$$$$$$$");
        Bukkit.getConsoleSender().sendMessage("§e$$$      $$$$ $$$$        $$$$$$$  $$$$$$$ $$$$$     $$$$");
        Bukkit.getConsoleSender().sendMessage("§e$$$$$$$$$$$$  $$$$$$$$$$$ $$$$$$$$$$$$$$$$ $$$$");
        Bukkit.getConsoleSender().sendMessage("§e$$$$$$$$$$$$  $$$$$$$$$$$ $$$$ $$$$$$ $$$$ $$$$");
        Bukkit.getConsoleSender().sendMessage("§e$$$      $$$$ $$$$        $$$$  $$$$  $$$$ $$$$$     $$$$");
        Bukkit.getConsoleSender().sendMessage("§e$$$$$$$$$$$$$ $$$$        $$$$        $$$$  $$$$$$$$$$$$$");
        Bukkit.getConsoleSender().sendMessage("§e$$$$$$$$$$$   $$$$        $$$$        $$$$   $$$$$$$$$$");
        Bukkit.getConsoleSender().sendMessage("§e=========================================================");
        Bukkit.getConsoleSender().sendMessage("§e作者：TV90，QQ：3449023732");
        Bukkit.getConsoleSender().sendMessage("§eBFMC服务器官群：750557809");
        Bukkit.getConsoleSender().sendMessage("§eBFMC基础功能插件已启用！");
        Bukkit.getConsoleSender().sendMessage("§e=========================================================");

        loadConfigurations();
        loadCommand();
        loadListener();
        setScheduler();
        loadSquad();
        saveDefaultConfig();
        reloadConfig();
        createGame();
        loadBattlepass();
    }

    private void loadBattlepass() {
        try {
            Class.forName("net.tv90.bfmccore.battlepass.Battlepass");
            Bukkit.getConsoleSender().sendMessage("§e战斗通行证模块已经加载");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDisable() {
        unLoadThreadPool();
        Battlepass.storeBattlepassData();
        Bukkit.getConsoleSender().sendMessage("§eBFMC基础功能插件已关闭！");
        HandlerList.unregisterAll();
    }
}